<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=777385
-->
<window title="Mozilla Bug 777385"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <div></div>
  <div id="mydivname"></div>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id="
     target="_blank">Mozilla Bug 777385</a>
  </body>

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[
  /** Test for Bug 777385 **/

  let Cu = Components.utils;

  let live_map = new WeakMap;

  let get_div_style = function () {
    return document.getElementById("mydivname").style;
  }

  let make_live_map = function () {
    let my_div_style = get_div_style();
    let div_fail = false;
    try {
      live_map.set(my_div_style, 12345);
    } catch (e) {
      div_fail = true;
    }
    ok(!div_fail, "Using elem.style as a weak map key should not produce an exception.");

    is(live_map.get(get_div_style()), 12345, "Live map should have live style with right value before GC.");

  }

  make_live_map();

  // RGBColor is a non-nsISupports refCounted class using WebIDL bindings.

  // non-nsISupports cycle-collected classes should fail as weak map keys.
  let context = window.getComputedStyle(document.documentElement, null).getPropertyCSSValue("color").getRGBColorValue();
  let contextFail = false;
  try {
    live_map.set(context, 2);
  } catch (e) {
    contextFail = true;
  }

  ok(contextFail, "Cycle collected non-nsISupports classes aren't allowed as weak map keys.");

  /* Set up for running precise GC/CC then check the results. */

  SimpleTest.waitForExplicitFinish();

  Cu.schedulePreciseGC(function () {
    SpecialPowers.DOMWindowUtils.cycleCollect();
    SpecialPowers.DOMWindowUtils.garbageCollect();
    SpecialPowers.DOMWindowUtils.garbageCollect();

    is(ThreadSafeChromeUtils.nondeterministicGetWeakMapKeys(live_map).length, 1,
       "Live nsISupports new DOM bindings wrappercached native weak map key should not be removed.");

    is(live_map.get(get_div_style()), 12345, "Live map should have live style with right value after GC.");

    SimpleTest.finish();
  });

  ]]>
  </script>
</window>
